#ifndef __UDPXBUFH__
#define __UDPXBUFH__

#include "caevent.h"
#include "casock.h"
#include "CGetPutBuf.h"
#include "calog.h"

#define MTU_DATA_SIZE 1460
#define SYNC_START 0xA5
#define SYNC_CONT 0x71
#define SYNC_ONE 0x55
#define SYNC_END 0xE5

#define SUB_SEQ_LIMIT 0x0f

#pragma pack(1)
typedef struct 
{
	unsigned char sync;
	unsigned char seq;
	uint32_t len;
} udpxhdr_t;
#pragma pack(0)


static int sockcb(sock_t* ps, int events);		

class CUdpXBuf {
	public:
		CUdpXBuf(es_t* psys, int bufsize, int mode, CGetPutBuf* gpbuf);
		virtual ~CUdpXBuf();

	private:
		sock_t sock, *psock;
		unsigned int curx;
		es_t*  psys;
		unsigned char* mData;
		unsigned char* mPacket;
		uint32_t mDataSize;
		int mChunkSize;
		int mPacketBufSize;
		unsigned char mFrameSeq;
		unsigned char mSubSeq;
		unsigned char mSync;
		CGetPutBuf* mGpBuf;
		int subx_status;
		int subx_wrcnt;
		int mPacketLen;
		int mTrans;
		int isDelayedPacket;
		
	private:		

		bool check_packet_seq(udpxhdr_t* phdr);
		void start_recv_packet(udpxhdr_t* phdr);
		void end_recv_packet(void);
		void discard_recv_packet(void);
		int build_packet(void);


	public:
		int connect(char* ip, int port);
		int listen(int port);
		void set(unsigned char* buf, int size, int csize);
		int SockProc(int events);
		int tx(void);
		int rx(void);
		void set_rx(unsigned char* buf, int bufsize, int chunksize);
		int status(void);
		void close(void);
		unsigned char* OnPrepareBuffer(unsigned int size);
		void OnReleaseBuffer(unsigned char* pbuf);
		void OnRecvFrame(unsigned char* frame);
		
		
};

#endif

